package com.company.project.web;

import com.company.project.application.service.IUserService;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by admin on 2018/08/29.
*/
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private IUserService IUserService;

    @PostMapping
    @ApiOperation(value = "添加单个值", notes = "添加单个值的接口")
    public Result add(@RequestBody User user) {
        IUserService.save(user);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个值", notes = "删除单个值的接口")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "主键id", required = true)
    })
    public Result delete(@PathVariable Integer id) {
        IUserService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "修改单个值", notes = "修改单个值的接口")
    public Result update(@RequestBody User user) {
        IUserService.update(user);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个值", notes = "获取单个值的接口")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "主键id", required = false)
    })
    public Result detail(@PathVariable Integer id) {
        User user = IUserService.findById(id);
        return ResultGenerator.genSuccessResult(user);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "分页获取所有列表", notes = "分页获取所有列表的接口")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "page", value = "分页值", required = false),
        @ApiImplicitParam(paramType = "query", name = "size", value = "分页大小", required = false)
    })
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        List<User> list = IUserService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
